<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProductSpec extends Model
{
    protected $guarded = ['id'];
    protected $appends = ['spec_price_txt','spec_group_price_txt','group_num','group_money','single_num','single_money','complete_num','grouped_num','all_num'];

    public function getSpecPriceTxtAttribute()
    {
    	return $this->spec_price/100;
    }

    public function getSpecGroupPriceTxtAttribute()
    {
    	return $this->spec_group_price/100;
    }

    /**
     * 成团数
     */
    public function getGroupedNumAttribute()
    {
        
        $where['spec_id'] = $this->id;
        $where['type']    = 2;
        $num = OrderProduct::where($where)
                            ->whereHas('order',function($query){
                                    $query->whereIn('status',[4,100]);
                                })
                            ->sum('product_num');
        return $num;
    }

    public function getGroupNumAttribute()
    {
        
        $where['spec_id'] = $this->id;
        $where['type']    = 2;
        $num = OrderProduct::where($where)
                            ->whereHas('order',function($query){
                                    $query->whereIn('status',['4','60','100']);
                                })
                            ->sum('product_num');
        return $num;
    }

    public function getGroupMoneyAttribute()
    {
        
        $where['spec_id'] = $this->id;
        $where['type']    = 2;
        $num = OrderProduct::where($where)
                            ->whereHas('order',function($query){
                                    $query->whereIn('status',['4','60','100']);
                                })
                            ->sum('subtotal');
        return $num/100;
    }

    public function getSingleNumAttribute()
    {
        
        $where['spec_id'] = $this->id;
        $where['type'] = 1;
        $num = OrderProduct::where($where)
                            ->whereHas('order',function($query){
                                    $query->where('status','>',1);
                                })
                            ->sum('product_num');
        return $num;
    }

     public function getSingleMoneyAttribute()
    {
        
        $where['spec_id'] = $this->id;
        $where['type']    = 1;
        $num = OrderProduct::where($where)
                            ->whereHas('order',function($query){
                                    $query->where('status','>',1);
                                })
                            ->sum('subtotal');
        return $num/100;
    }

    public function getCompleteNumAttribute()
    {
        
        $where['spec_id'] = $this->id;
        $where['type'] = 2;
        $num = OrderProduct::where($where)
                            ->whereHas('order',function($query){
                                    $query->where('status',100);
                                })
                            ->sum('product_num');
        return $num;
    }

     public function getAllNumAttribute()
    {
        
        $where['spec_id'] = $this->id;
        $num = OrderProduct::where($where)
                            ->whereHas('order',function($query){
                                    $query->where('status','>',2);
                                })
                            ->sum('product_num');
        return $num;
    }



    
}
